package com.cloudspace.jindun.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.xiaoxi.GroupchatActivity;
import com.cloudspace.jindun.activity.xiaoxi.HistoricalActivity;
import com.cloudspace.jindun.activity.xiaoxi.TopicActivity;
import com.cloudspace.jindun.adapter.ConversationListAdapterEx;
import com.cloudspace.jindun.constants.Constants;
import com.cloudspace.jindun.fragment.xiaoxi.TopicFragment;
import com.cloudspace.jindun.fragment.xiaoxi.GroupChatFragment;
import com.cloudspace.jindun.utils.AppPreferences;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongContext;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;


public class MessageFragment extends BaseFragment {
    private RadioGroup mRadioGroup;
    public static ViewPager mViewPager;
    private FragmentPagerAdapter mFragmentPagerAdapter;
    private Fragment mConversationFragment = null;
    private Fragment mConversationsLiast;
    private ImageView mAdd;
    private List<Fragment> mFragment = new ArrayList<>();
    private RelativeLayout mMenu;
    private TextView menuTVlishi;
    private TextView menuTvfaqi;
    private boolean istype = true;
    private int isbut = 2;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_xiaoxi, container, false);
        mConversationsLiast = initConversationList();
        initView(rootView);
        setListener();
        return rootView;
    }

    private void setListener() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton) mRadioGroup.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        AppPreferences.putBoolean(Constants.GROUPCHAT_ON, false);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int count = group.getChildCount();
                for (int i = 0; i < count; i++) {
                    if (i == 0) {
                        mAdd.setVisibility(View.GONE);
                    } else {
                        mAdd.setVisibility(View.VISIBLE);
                    }
                    if (i == 1) {
                        AppPreferences.putBoolean(Constants.GROUPCHAT_ON, false);
                        menuTvfaqi.setText(getResources().getString(R.string.start_topic));
                        menuTVlishi.setText(getResources().getString(R.string.history_topic));
                        isbut = 1;
                    }
                    if (i == 2) {
                        AppPreferences.putBoolean(Constants.GROUPCHAT_ON, true);
                        menuTvfaqi.setText(getResources().getString(R.string.start_group_chat));
                        menuTVlishi.setText(getResources().getString(R.string.join_group_chat));
                        isbut = 2;
                    }
                    RadioButton button = (RadioButton) group.getChildAt(i);
                    if (button.isChecked()) {
                        //如果选中当前的i 就是选中的位置
                        if (!istype) {
                            mMenu.setVisibility(View.GONE);
                            istype = true;
                        }
                        mViewPager.setCurrentItem(i);
                        break;
                    }
                }
            }
        });

        mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (istype) {
                    mMenu.setVisibility(View.VISIBLE);
                    istype = false;
                } else {
                    mMenu.setVisibility(View.GONE);
                    istype = true;
                }
            }
        });
        menuTvfaqi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("isbut", isbut);
                intent.setClass(getContext(), TopicActivity.class);
                startActivity(intent);
            }
        });
        menuTVlishi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isbut == 1) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), HistoricalActivity.class);
                    startActivity(intent);
                }
                if (isbut == 2) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), GroupchatActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private void initView(View rootView) {
        mRadioGroup = (RadioGroup) rootView.findViewById(R.id.xiaoxi_radioGroup);
        mMenu = (RelativeLayout) rootView.findViewById(R.id.xiaoxi_rl_menu);
        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
        mViewPager = (ViewPager) rootView.findViewById(R.id.main_viewpager);
        mAdd = (ImageView) rootView.findViewById(R.id.xiaoxi_iv_add);
        menuTvfaqi = (TextView) rootView.findViewById(R.id.xiaoxi_tv_fqht);
        menuTVlishi = (TextView) rootView.findViewById(R.id.xiaoxi_tv_lsht);
        mFragment.add(mConversationsLiast);
        mFragment.add(TopicFragment.getInstance());
        // mFragment.add(mConversationsQH);
        mFragment.add(GroupChatFragment.getInstance());
        mFragmentPagerAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return mFragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }
        };
        mViewPager.setAdapter(mFragmentPagerAdapter);
//        mViewPager.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//
//                return true;
//            }
//        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onFragmentCreated(Bundle savedInstanceState) {

    }

    @Override
    public void updateContent() {

    }

    private Fragment initConversationList() {
        if (mConversationFragment == null) {
            ConversationListFragment listFragment = new ConversationListFragment();
            listFragment.setAdapter(new ConversationListAdapterEx(RongContext.getInstance()));
            Uri uri;
            uri = Uri.parse("rong://" + getActivity().getPackageName()).buildUpon()
                    .appendPath("conversationlist")
                    .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                    .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                    .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                    .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                    .build();


            listFragment.setUri(uri);
            return listFragment;
        } else {
            return mConversationFragment;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!istype) {
            mMenu.setVisibility(View.GONE);
            istype = true;
        }


    }
}
